﻿using Model;
using Business.IFace;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MB.Orm.DB;
using Public.DataAccess;
using MB.RuleBase.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;

namespace Business
{
    public class BGoodsMove
    {

        public int AddGoodsMove(GoodsMove goodsmove)
        {
            return DatabaseExcuteByXmlHelper.NewInstance.ExecuteNonQueryByEntity<GoodsMove>
                            ("GoodsMove", "AddGoodsMove", goodsmove);
        }
        public int EditGoodsMoveByID(GoodsMove goodsmove)
        {
            return DatabaseExcuteByXmlHelper.NewInstance.ExecuteNonQueryByEntity<GoodsMove>
                            ("GoodsMove", "EditGoodsMoveByID", goodsmove);
        }
        public List<GoodsMove> GetGoodsMoveList(int pageIndex, int pageSize, out int total, params object[] parValues)
        {
            using (var DbScope = new OperationDatabaseScope(new OperationDatabaseContext("NewWeb")))
            {
                List<GoodsMove> list = PagedDatabaseExcuteByXmlHelper.NewInstance.GetPagedObjectsByXml3<GoodsMove>
                                                ("GoodsMove", "GetGoodsMoveInfo", pageIndex, pageSize, out total, parValues);
                return list;
            }
        }
        public List<GoodsMoveItem> GetGoodsMoveItemByGoodsMoveID(string GoodsMoveID)
        {
            using (var DbScope = new OperationDatabaseScope(new OperationDatabaseContext("NewWeb")))
            {
                List<GoodsMoveItem> list = DatabaseExcuteByXmlHelper.NewInstance.GetObjectsByXml<GoodsMoveItem>
                                                ("GoodsMove", "GetGoodsMoveItemByGoodsMoveID", GoodsMoveID);
                return list;
            }
        }
        public int DeleteGoodsMove(string GoodsMoveID)
        {
            return DatabaseExcuteByXmlHelper.NewInstance.ExecuteNonQuery("GoodsMove", "DeleteGoodsMove", GoodsMoveID);
        }
        public List<GoodsMoveItem> GetGoodsMoveItemList(int pageIndex, int pageSize, out int total, params object[] parValues)
        {
            using (var DbScope = new OperationDatabaseScope(new OperationDatabaseContext("NewWeb")))
            {
                List<GoodsMoveItem> list = PagedDatabaseExcuteByXmlHelper.NewInstance.GetPagedObjectsByXml3<GoodsMoveItem>
                                                ("GoodsMove", "GetGoodsMoveItemInfo", pageIndex, pageSize, out total, parValues);
                return list;
            }
        }
        public int AddGoodsMoveItem(GoodsMoveItem goodsmoveitem)
        {
            return DatabaseExcuteByXmlHelper.NewInstance.ExecuteNonQueryByEntity<GoodsMoveItem>
                            ("GoodsMove", "AddGoodsMoveItem", goodsmoveitem);
        }
        public int EditGoodsMoveItemByID(GoodsMoveItem goodsmoveitem)
        {
            return DatabaseExcuteByXmlHelper.NewInstance.ExecuteNonQueryByEntity<GoodsMoveItem>
                            ("GoodsMove", "EditGoodsMoveItemByID", goodsmoveitem);
        }
        public int DeleteGoodsMoveItem(string SEQNO)
        {
            return DatabaseExcuteByXmlHelper.NewInstance.ExecuteNonQuery("GoodsMove", "DeleteGoodsMoveItem", SEQNO);
        }

        public bool AuditGoodsMove(string GoodsMoveID, AppUserList model)
        {
            bool result = false;
            List<LocationInventoryInfo> _lstFromLocation = new List<LocationInventoryInfo>();
            List<LocationInventoryInfo> _lstToLocation = new List<LocationInventoryInfo>();
            int total = 0;
            string where = " where  g.GOODSMOVEID='" + GoodsMoveID + "'";
            List<GoodsMove> lstGoodsMove = this.GetGoodsMoveList(1, 1, out total, where);
            GoodsMove _GoodsMove = lstGoodsMove[0];

            List<GoodsMoveItem> lstGoodsMoveItem = this.GetGoodsMoveItemByGoodsMoveID(GoodsMoveID);

            //定义事物
            var oracle = new MB.RuleBase.BulkCopy.SimulatedOracleHelper();
            var db = MB.Orm.Persistence.DatabaseHelper.CreateDatabase();
            var cn = oracle.CreateOracleConnection(db.ConnectionString);
            DbTransaction tran = null;
            try
            {
                // 打开数据连接事物
                cn.Open();
                tran = cn.BeginTransaction();
                _GoodsMove.FLAG = "100";
                _GoodsMove.EDITOR = model.USERNAME;
                _GoodsMove.EDITDATE = DateTime.Now;
                foreach (GoodsMoveItem _GoodsMoveItem in lstGoodsMoveItem)
                {
                    LocationInventoryInfo _FromLocation = new LocationInventoryInfo();
                    _FromLocation.GOODSBATCHCODE = _GoodsMoveItem.GOODSBATCHCODE;
                    _FromLocation.LOCATIONID = _GoodsMoveItem.FROMLOCATIONID;
                    _FromLocation.WAREHOUSEID = _GoodsMove.WAREHOUSEID;
                    _FromLocation.CELLNO = _GoodsMove.CELLNO;
                    _FromLocation.GOODSID = _GoodsMoveItem.GOODSID;
                    _FromLocation.PRODUCTIONDATE = _FromLocation.GOODSBATCHCODE.Substring(8, 6);
                    try
                    {
                        _FromLocation.EXPIRYDAYS = Convert.ToInt32(_FromLocation.GOODSBATCHCODE.Substring(15, 3));
                    }
                    catch
                    {
                        _FromLocation.EXPIRYDAYS = 0;
                    }
                    _FromLocation.QTY = -_GoodsMoveItem.QTY;
                    _lstFromLocation.Add(_FromLocation);

                    LocationInventoryInfo _ToLocation = new LocationInventoryInfo();
                    _ToLocation.GOODSBATCHCODE = _GoodsMoveItem.GOODSBATCHCODE;
                    _ToLocation.LOCATIONID = _GoodsMoveItem.TOLOCATIONID;
                    _ToLocation.WAREHOUSEID = _GoodsMove.WAREHOUSEID;
                    _ToLocation.CELLNO = _GoodsMove.CELLNO;
                    _ToLocation.GOODSID = _GoodsMoveItem.GOODSID;
                    _ToLocation.PRODUCTIONDATE = _FromLocation.PRODUCTIONDATE;
                    _ToLocation.EXPIRYDAYS = _FromLocation.EXPIRYDAYS;
                    _ToLocation.QTY = _GoodsMoveItem.QTY;
                    _lstToLocation.Add(_ToLocation);

                }
                //写货位库存 写货位库存事务 更新货位搬移状态
                result = this.InsertLocationInventory(tran, _lstFromLocation);
                result = result && this.InsertLocationInventory(tran, _lstToLocation);
                result = result && this.InsertLocationTranByFrom(tran, _GoodsMove.GOODSMOVEID);
                result = result && this.InsertLocationTranByTo(tran, _GoodsMove.GOODSMOVEID);
                result = result && DatabaseExcuteByXmlHelper.NewInstance.ExecuteNonQueryByEntity<GoodsMove>
                            (tran, "GoodsMove", "EditGoodsMoveByID", lstGoodsMove)>0;
                //this.EditGoodsMoveByID(_GoodsMove);
                if (result)
                    tran.Commit();
                else
                    tran.Rollback();
                return result;
            }
            catch (Exception ex)
            {
                MB.Util.TraceEx.Write(ex.Message);
                return false;
            }
            finally
            {
                if (tran != null) tran.Dispose();
                if (cn != null) cn.Close();
                if (cn != null) cn.Dispose();
            }
        }
        
        //记录货位库存
        public bool InsertLocationInventory(DbTransaction transaction, List<LocationInventoryInfo> lstLocationInventory)
        {
            try
            {
                if (lstLocationInventory == null || lstLocationInventory.Count <= 0) return false;

                using (MB.RuleBase.BulkCopy.IDbBulkExecute bulk = MB.RuleBase.BulkCopy.DbBulkExecuteFactory.CreateDbBulkExecute(transaction))
                {
                    bulk.WriteToServer("GoodsMove", "InsertLocationInventory", lstLocationInventory);
                }

                return true;
            }
            catch (Exception ex)
            {
                MB.Util.TraceEx.Write(ex.Message);
                return false;
            }
        }

        //记录货位库存事物
        public bool InsertLocationTranByFrom(DbTransaction transaction, string goodsMoveID)
        {
            try
            {
                DatabaseExcuteByXmlHelper.NewInstance.ExecuteNonQuery(transaction, "GoodsMove", "InsertLocationTranByFrom", goodsMoveID);

                return true;
            }
            catch (Exception ex)
            {
                MB.Util.TraceEx.Write(ex.Message);
                return false;
            }
        }

        //记录货位库存事物
        public bool InsertLocationTranByTo(DbTransaction transaction, string goodsMoveID)
        {
            try
            {
                DatabaseExcuteByXmlHelper.NewInstance.ExecuteNonQuery(transaction, "GoodsMove", "InsertLocationTranByTo", goodsMoveID);

                return true;
            }
            catch (Exception ex)
            {
                MB.Util.TraceEx.Write(ex.Message);
                return false;
            }
        }

        public int DeleteGoodsMoveItemByGoodsMoveID(string GoodsMoveID)
        {
            return DatabaseExcuteByXmlHelper.NewInstance.ExecuteNonQuery("GoodsMove", "DeleteGoodsMoveItemByGoodsMoveID", GoodsMoveID);
        }
    }
}
